// file for general purpose javascript javascript support

//////////////////////////////////////////////////////////////////////////////////////////
// for the stripped down window for admin report generation
function popwin(url) {
   window.open(url,"","height=600,width=1000,menubar=no,toolbar=no,location=no,scrollbars=yes,resizable=yes,status=no");
}

//////////////////////////////////////////////////////////////////////////////////////////
// used for printing reviews on public side
function popwin2(url) {
   win = window.open(url,"","height=600,width=1000,menubar=no,toolbar=no,location=no,scrollbars=yes,resizable=yes,status=no");
   // This probably only works in firefox
   //win.onblur = function() {win.close();}
   // Dan didt like this effect for the reports anyway
}


//////////////////////////////////////////////////////////////////////////////////////////
// grab select content, put in new window, print it
// used in review_edit.php
function printSpecial() {
	if (document.getElementById != null) {
		var html = '<HTML>\n<HEAD>\n';

		if (document.getElementsByTagName != null) {
			var headTags = document.getElementsByTagName("head");
			if (headTags.length > 0)
				html += headTags[0].innerHTML;
		}

		html += '\n</HE' + 'AD>\n<BODY>\n';

		var printReadyElem = document.getElementById("printReady");

		if (printReadyElem != null) {
				html += printReadyElem.innerHTML;
		}
		else {
			alert("Could not find the printReady section in the HTML");
			return;
		}

		html += '\n</BO' + 'DY>\n</HT' + 'ML>';

		var printWin = window.open("","printSpecial");
		printWin.document.open();
		printWin.document.write(html);
		printWin.document.close();
		printWin.print();
		//printWin.close();  // In IE this kills the window before the print dialog comes up
		printWin.onblur = function() {printWin.close();}
	}
	else {
		alert("Sorry, the print ready feature is only available in modern browsers.");
	}
}



//////////////////////////////////////////////////////////////////////////////////////////
function CountWords (this_field, show_word_count, show_char_count, max_value) {
    if (show_word_count == null) {
        show_word_count = true;
    }
    if (show_char_count == null) {
        show_char_count = false;
    }
    var char_count = this_field.value.length;
    var fullStr = this_field.value + " ";
    var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
    var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
    var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9-/]+/gi;
    var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
    var splitString = cleanedStr.split(" ");
    var word_count = splitString.length -1;
    if (fullStr.length <2) {
        word_count = 0;
    }
    if (word_count == 1) {
        wordOrWords = " word";
    }
    else {
        wordOrWords = " words";
    }
    if (char_count == 1) {
        charOrChars = " character";
    } else {
        charOrChars = " characters";
    }
    if (show_word_count & show_char_count) {
        alert ("Word Count:\n" + "    " + word_count + wordOrWords + "\n" + "    " + char_count + charOrChars);
    }
    else {
        if (show_word_count && word_count > 0) {
             //alert ("Word Count:  " + word_count + wordOrWords);
             var the_span = document.getElementById('word_count');
             the_span.innerHTML = "(Current count: " + word_count + wordOrWords + ")";
             if (word_count > max_value) {
                the_span.style.color = '#F00';
             }
             else {
                the_span.style.color = '#000';
             }
        }
        else {
            if (show_char_count) {
                alert ("Character Count:  " + char_count + charOrChars);
            }
        }
    }
    return word_count;
}






